import sys
import os
import random
from sqlalchemy.orm import Session
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))

from app.db import get_db, init_db
from app.models.models import Student, Preference

# 预定义的兴趣标签列表
INTEREST_TAGS = [
    "运动", "音乐", "游戏", "阅读", "电影", "旅行", "摄影", "美食",
    "编程", "绘画", "舞蹈", "篮球", "足球", "动漫", "宠物", "烹饪"
]

def add_random_interests():
    db = next(get_db())
    try:
        # 获取所有学生
        students = db.query(Student).all()
        print(f"找到 {len(students)} 个学生")

        for student in students:
            # 为每个学生随机选择3个兴趣标签
            selected_tags = random.sample(INTEREST_TAGS, 3)
            
            # 更新或创建学生的偏好设置
            preference = db.query(Preference).filter(Preference.student_id == student.id).first()
            if not preference:
                preference = Preference(student_id=student.id)
                db.add(preference)
            
            preference.interest_tags = selected_tags
            print(f"为学生 {student.name}({student.student_id}) 添加兴趣标签: {', '.join(selected_tags)}")

        db.commit()
        print("成功为所有学生添加随机兴趣标签！")

    except Exception as e:
        print(f"发生错误: {str(e)}")
        db.rollback()
    finally:
        db.close()

if __name__ == "__main__":
    init_db()
    add_random_interests() 